package eyrolles;

import exceptions.SymboleException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

//import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class LookupAction extends Action {

    protected Connection getConnection() throws ClassNotFoundException, SQLException {
        Properties prop = (Properties) servlet.getServletContext().getAttribute("PROPERTIES");
        if (prop == null) {
            return null;
        }
        String driver = prop.getProperty("driver");
        String db = prop.getProperty("db");
        String login = prop.getProperty("login");
        String pass = prop.getProperty("pass");

        Class.forName(driver);

        return DriverManager.getConnection(db, login, pass);
    }

    protected Double getCotation(String symbole) throws SQLException, SymboleException {

        Double cours = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            if ((conn = getConnection()) == null) {
                return 0.0;
            }
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from cotations where " + "symbole='" + symbole + "'");

            if (rs.next()) {

                double tmp = 0;
                tmp = rs.getDouble("cours");
                cours = new Double(tmp);

            } else {
                throw new SymboleException();
            }
        } catch (ClassNotFoundException e) {

            throw new SQLException();
        } finally {
            if (rs != null) {

                rs.close();
                rs = null;
            }
            if (stmt != null) {

                stmt.close();
                stmt = null;
            }
            if (conn != null) {

                conn.close();
                conn = null;
            }
        }
        return cours;
    }

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        ActionMessages erreurs = new ActionMessages();
        Double cours = null;

        // Cible par defaut
        String cible = new String("succes");
        String symbole = null;

        if (form != null) {

            // Utilisation de LookupForm pour obtenir les parametres de la requete
            LookupForm lookupForm = (LookupForm) form;

            symbole = lookupForm.getSymbole();

            try {
                cours = getCotation(symbole);
                request.setAttribute("COURS", cours);

            } catch (SymboleException s) {

                cible = new String("echec");
                erreurs.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("errors.recherche.symbole.inconnu", symbole));

            } catch (SQLException e) {
                cible = new String("echec");
                erreurs.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("errors.database.error"));
            }
        }



        // Signalement des erreurs a la page d'origine
        if (!erreurs.isEmpty()) {

            saveErrors(request, erreurs);
        }
        
        // Transmission a la vue appropriee
        return (mapping.findForward(cible));
    }
}
